package com.xuexiang.xuidemo.slice.progress;

import ohos.agp.components.Component;
import ohos.agp.components.Text;

import com.xuexiang.xui_lib.component.progress.CircleProgressView;
import com.xuexiang.xui_lib.component.progress.HorizontalProgressView;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;

public class BeautifulProgressBarSlice extends BaseAbilitySlice implements HorizontalProgressView.HorizontalProgressUpdateListener,CircleProgressView.CircleProgressUpdateListener{

    CircleProgressView progressViewCircleSmall;
    CircleProgressView progressViewCircleMain;

    Text progressTextMain;
    HorizontalProgressView hpvLanguage;
    Text progressTextLanguage;
    HorizontalProgressView hpvHistory;
    Text progressTextHistory;
    HorizontalProgressView hpvMath;
    Text progressTextMath;
    HorizontalProgressView hpvEnglish;
    Text progressTextEnglish;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_slice_beautiful_progressbar;
    }

    @Override
    protected void initComponents() {
        super.initComponents();
        initView();
    }

    public void initView(){
        progressViewCircleSmall = (CircleProgressView) findComponentById(ResourceTable.Id_progressView_circle_small);
        progressViewCircleMain = (CircleProgressView) findComponentById(ResourceTable.Id_progressView_circle_main);
        progressTextMain = (Text) findComponentById(ResourceTable.Id_progress_text_main);
        hpvLanguage = (HorizontalProgressView) findComponentById(ResourceTable.Id_hpv_language);
        progressTextLanguage = (Text) findComponentById(ResourceTable.Id_progress_text_language);
        hpvHistory = (HorizontalProgressView) findComponentById(ResourceTable.Id_hpv_history);
        progressTextHistory = (Text) findComponentById(ResourceTable.Id_progress_text_history);
        hpvMath = (HorizontalProgressView) findComponentById(ResourceTable.Id_hpv_math);
        progressTextMath = (Text) findComponentById(ResourceTable.Id_progress_text_math);
        hpvEnglish = (HorizontalProgressView) findComponentById(ResourceTable.Id_hpv_english);
        progressTextEnglish = (Text) findComponentById(ResourceTable.Id_progress_text_english);


        hpvLanguage.setProgressViewUpdateListener(this);
        hpvMath.setProgressViewUpdateListener(this);
        hpvHistory.setProgressViewUpdateListener(this);
        hpvEnglish.setProgressViewUpdateListener(this);
        progressViewCircleSmall.setProgressViewUpdateListener(this);

        progressViewCircleMain.setGraduatedEnabled(true);
        progressViewCircleMain.setProgressViewUpdateListener(this);

        findComponentById(ResourceTable.Id_btn_start).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                hpvLanguage.startProgressAnimation();
                hpvMath.startProgressAnimation();
                hpvHistory.startProgressAnimation();
                hpvEnglish.startProgressAnimation();
            }
        });

        findComponentById(ResourceTable.Id_slice_arcloadingview_backImage).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
    }


    /**
     * 进度条开始更新
     *
     * @param view
     */
    @Override
    public void onCircleProgressStart(Component view) {

    }

    /**
     * 进度条更新中
     *
     * @param view
     * @param progress
     */
    @Override
    public void onCircleProgressUpdate(Component view, float progress) {
        int progressInt = (int) progress;
        if (view.getId() == ResourceTable.Id_progressView_circle_main) {
            progressTextMain.setText(progressInt + "");
        }
    }

    /**
     * 进度条更新结束
     *
     * @param view
     */
    @Override
    public void onCircleProgressFinished(Component view) {
        if (view.getId() == ResourceTable.Id_progressView_circle_small) {
            progressViewCircleMain.startProgressAnimation();
        }
    }

    @Override
    public void onHorizontalProgressStart(Component view) {

    }

    @Override
    public void onHorizontalProgressUpdate(Component view, float progress) {
        int progressInt = (int) progress;
        switch (view.getId()) {
            case ResourceTable.Id_hpv_language:
                progressTextLanguage.setText(progressInt + "%");
                break;
            case ResourceTable.Id_hpv_english:
                progressTextEnglish.setText(progressInt + "%");
                break;
            case ResourceTable.Id_hpv_history:
                progressTextHistory.setText(progressInt + "%");
                break;
            case ResourceTable.Id_hpv_math:
                progressTextMath.setText(progressInt + "%");
                break;
            default:
                break;
        }
    }

    @Override
    public void onHorizontalProgressFinished(Component view) {
        if (view.getId() == ResourceTable.Id_hpv_language) {
            progressViewCircleSmall.startProgressAnimation();
        }
    }

    @Override
    protected void onBackground() {
        progressViewCircleSmall.stopProgressAnimation();
        progressViewCircleMain.stopProgressAnimation();

        hpvLanguage.stopProgressAnimation();
        hpvMath.stopProgressAnimation();
        hpvHistory.stopProgressAnimation();
        hpvEnglish.stopProgressAnimation();


        progressViewCircleSmall.setProgressViewUpdateListener(null);
        progressViewCircleMain.setProgressViewUpdateListener(null);

        hpvLanguage.setProgressViewUpdateListener(null);
        hpvMath.setProgressViewUpdateListener(null);
        hpvHistory.setProgressViewUpdateListener(null);
        hpvEnglish.setProgressViewUpdateListener(null);
        super.onBackground();
    }


}
